package com.infitecs.eyas.quality.service.Imp;

import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.mill.dao.MillSelectDao;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.BurstDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.quality.service.AshService;
import com.infitecs.eyas.quality.service.BurstService;
import com.infitecs.eyas.quality.service.ElementService;
import com.infitecs.eyas.quality.service.IndustryService;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerDao;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.runcoalbin.service.CoalBunkerService;
import com.infitecs.eyas.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.infitecs.eyas.constant.Constant;
import org.xidea.el.Expression;
import org.xidea.el.impl.ExpressionImpl;

import javax.print.Doc;
import java.util.HashMap;

/**
 * Created by Demon on 2015/4/23.
 */
@Service("industryService")
@Transactional
public class IndustryServiceImp implements IndustryService {
    @Autowired
    private ExpressionDao expressionDao;
    @Autowired
    private IndustryDao industryDao;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private ElementService elementService;
    @Autowired
    private ElementDao elementDao;
    @Autowired
    private AshDao ashDao;
    @Autowired
    private AshService ashService;
    @Autowired
    private BurstService burstService;
    @Autowired
    private BurstDao burstDao;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private EfficiencyService efficiencyService;
    @Autowired
    private CoalBunkerDao coalBunkerDao;
    @Autowired
    private CoalBunkerService coalBunkerService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private BurnDao burnDao;
    @Autowired
    private CleanService cleanService;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private ChimneyService chimneyService;
    @Autowired
    private MillSelectDao millSelectDao;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private PipeCalculateService pipeCalculateService;
    @Autowired
    private PipeCalculateDao pipeCalculateDao;

    public Double calculateMfByFormula15(int caseId) {
        HashMap<String, Double> map = new HashMap<String, Double>();
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        map.put("E3", industry.getMar());
        map.put("E4", industry.getMad());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_15);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Double mf = DoubleUtil.getInstance().get2Double(Double.valueOf(expression.evaluate(map).toString()));
        industryDao.updateMf(mf, industry.getPowerCaseId());
        return mf;
    }

    @Override
    public Double calculateVdafByFormula20(int caseId) {
        HashMap<String, Double> params = new HashMap<String, Double>();
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        params.put("E3", industry.getMar());
        params.put("E6", industry.getAar());
        params.put("E9", industry.getVar());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_20);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Double vdaf = DoubleUtil.getInstance().get2Double(Double.valueOf(expression.evaluate(params).toString()));
        industryDao.updateVdaf(vdaf, caseId);
        return vdaf;
    }

    @Override
    public Double calculateVarByFormula19(int caseId) {
        HashMap<String, Double> params = new HashMap<String, Double>();
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        params.put("E3", industry.getMar());
        params.put("E6", industry.getAar());
        params.put("E10", industry.getVdaf());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_19);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Double var = DoubleUtil.getInstance().get2Double(Double.valueOf(expression.evaluate(params).toString()));
        industryDao.updateVar(var, caseId);
        return var;
    }

    @Override
    public void saveIndustry(Industry industry) {
        industryDao.updateIndustry(industry);
    }

    @Override
    public void updateMar(double mar, int caseId) {
        mar = DoubleUtil.getInstance().get2Double(mar);
        industryDao.updateMar(mar, caseId);
    }

    @Override
    public void updateMad(double mad, int caseId) {
        mad = DoubleUtil.getInstance().get2Double(mad);
        industryDao.updateMad(mad, caseId);
    }

    @Override
    public void updateAar(double aar, int caseId) {
        aar = DoubleUtil.getInstance().get2Double(aar);
        industryDao.updateAar(aar, caseId);
    }

    @Override
    public void updateMf(double mf, int caseId) {
        mf = DoubleUtil.getInstance().get2Double(mf);
        industryDao.updateMf(mf, caseId);
    }

    @Override
    public void updateVdaf(double vdaf, int caseId) {
        vdaf = DoubleUtil.getInstance().get2Double(vdaf);
        industryDao.updateVdaf(vdaf, caseId);
    }

    @Override
    public void updateVar(double var, int caseId) {
        var = DoubleUtil.getInstance().get2Double(var);
        industryDao.updateVar(var, caseId);
    }

    @Override
    public Industry getIndustry(int caseId) {
        return industryDao.getIndustryByPowerCaseId(caseId);
    }

    @Override
    public void industrySave(Industry industry) {
        //重新计算工业分析
        industrySaveData(industry);
        OperateField operateField = operateFieldDao.getOperateField(industry.getPowerCaseId());
        //更新元素分析
        Element oldElement = elementDao.getElementByPowerCaseId(industry.getPowerCaseId());
        if (oldElement == null) return;
        oldElement.setQnetArState(operateField.getQnetArState());
        oldElement.setQnetArOtherState(operateField.getQnetArOtherState());
        oldElement.setQgrArState(operateField.getQgrArState());
        oldElement.setQgrArOtherState(operateField.getQgrArOtherState());
        elementService.elementSaveData(oldElement);
        //更新灰分分析
        Ash oldAsh = ashDao.getAshByPowerCaseId(industry.getPowerCaseId());
        if (oldAsh == null) return;
        oldAsh.setOtherState(operateField.getOtherState());
        ashService.ashSaveData(oldAsh);
        //更新磨损腐蚀爆炸性
        Burst oldBurst = burstDao.getBurstByPowerCaseId(industry.getPowerCaseId());
        if (oldBurst == null) return;
        burstService.boomSaveData(oldBurst);
        //更新锅炉效率及燃煤量
        Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(industry.getPowerCaseId());
        if (oldEfficiency != null) {
            oldEfficiency.setE52State(operateField.getE52State());
            oldEfficiency.setE89State(operateField.getE89State());
            oldEfficiency.setE90State(operateField.getE90State());
            oldEfficiency.setE92State(operateField.getE92State());
            oldEfficiency.setE93State(operateField.getE93State());
            oldEfficiency.setE94State(operateField.getE94State());
            oldEfficiency.setE95State(operateField.getE95State());
            efficiencyService.efficiencySaveData(oldEfficiency);
        }
        //更新原煤仓
        CoalBunker oldCoalBunker = coalBunkerDao.getCoalBunker(industry.getPowerCaseId());
        //更新原煤仓信息
        if (oldCoalBunker != null) {
            oldCoalBunker.setE9State(operateField.getE9State());
            coalBunkerService.designSaveData(oldCoalBunker);
        }
        //更新燃烧
        CalculateBurn calculateBurn = burnDao.getCalculateBurnByCaseId(industry.getPowerCaseId());
        if (calculateBurn != null) {
            calculateBurn.setYyglState(operateField.getYyglState());
            calculateBurn.setE54State(operateField.getE54State());
            calculateBurn.setE88State(operateField.getE88State());
            calculateBurn.setE122State(operateField.getE122State());
            calculateBurn.setE155State(operateField.getE155State());
            calculateBurn.setE171State(operateField.getE171State());
            burnService.burnSaveData(calculateBurn);
        }
        //更新烟气净化系统
        CalculateClean calculateClean = cleanDao.getCalculateClean(industry.getPowerCaseId());
        if (calculateClean != null) {
            calculateClean.setE221State(operateField.getE221State());
            calculateClean.setE222State(operateField.getE222State());
            calculateClean.setE231State(operateField.getE231State());
            calculateClean.setE241State(operateField.getE241State());
            calculateClean.setE242State(operateField.getE242State());
            calculateClean.setE248State(operateField.getE248State());
            calculateClean.setE258State(operateField.getE258State());
            cleanService.cleanDataSave(calculateClean);
        }
        //更新烟囱出口直径计算
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(industry.getPowerCaseId());
        if (calculateChimney != null) {
            calculateChimney.setE314State(operateField.getE314State());
            calculateChimney.setE315State(operateField.getE315State());
            calculateChimney.setE316State(operateField.getE316State());
            calculateChimney.setCaseId(operateField.getCaseId());
            chimneyService.chimneySaveData(calculateChimney);
        }
        //更新磨煤机选择
        MillSelect millSelect = millSelectDao.getMillSelect(industry.getPowerCaseId());
        if (millSelect != null) {
            millSelect.setMyglState(operateField.getMyglState());
            millSelect.setE12State(operateField.getE12State());
            millSelect.setE74State(operateField.getE74State());
            millSelect.setE75State(operateField.getE75State());
            millSelect.setModelState(operateField.getModelState());
            millSelectService.chooseDataSave(millSelect);
        }
        //更新煤粉管道计算
        PipeCalculate pipeCalculate = pipeCalculateDao.getPipeCalculate(industry.getPowerCaseId());
        if (pipeCalculate != null) {
            pipeCalculateService.pipelineDataSave(pipeCalculate);
        }

    }

    private void industrySaveData(Industry industry) {
        //计算外在水分MF(%)
        double mf = calculateMfByFormula15(industry);
        industry.setMf(mf);
        //干燥无灰基挥发分Vdaf(%)计算
        if ("0".equals(industry.getVdafState())) {
            double vdaf = calculateVdafByFormula20(industry);
            industry.setVdaf(vdaf);
        }
        //收到基挥发分Var(%)计算
        if ("0".equals(industry.getVarState())) {
            double var = calculateVarByFormula19(industry);
            industry.setVar(var);
        }
        Industry oldIndustry = industryDao.getIndustryByPowerCaseId(industry.getPowerCaseId());
        //校验新增或者编辑
        if (oldIndustry == null) {
            industryDao.addIndustry(industry);
        } else {
            industryDao.updateIndustry(industry);
        }
        //更新只读或者修改状态
        OperateField operateField = new OperateField();
        operateField.setVarState(industry.getVarState());
        operateField.setVdafState(industry.getVdafState());
        operateField.setCaseId(industry.getPowerCaseId());
        operateFieldDao.updateOperateFieldIndustry(operateField);
    }

    public Double calculateMfByFormula15(Industry industry) {
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E3", industry.getMar());
        map.put("E4", industry.getMad());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_15);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression.evaluate(map).toString()));
    }

    public Double calculateVdafByFormula20(Industry industry) {
        HashMap<String, Double> params = new HashMap<String, Double>();
        params.put("E3", industry.getMar());
        params.put("E6", industry.getAar());
        params.put("E9", industry.getVar());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_20);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression.evaluate(params).toString()));
    }

    public Double calculateVarByFormula19(Industry industry) {
        HashMap<String, Double> params = new HashMap<String, Double>();
        params.put("E3", industry.getMar());
        params.put("E6", industry.getAar());
        params.put("E10", industry.getVdaf());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_19);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression.evaluate(params).toString()));
    }

}
